import request from "@/utils/request";
import type {
  GetRoleListApiParams,
  GetRoleListApiResponse,
  UpdateRoleApiParams,
  GetRolePermissionListApiResponse,
} from "./model/roleModel";
enum Api {
  getRoleList = "/admin/acl/role",
  addRole = "/admin/acl/role/save",
  updateRole = "/admin/acl/role/update",
  removeRole = "/admin/acl/role/remove",
  getRolePermissionList = "/admin/acl/permission/toAssign",
  assignRolePermissionList = "/admin/acl/permission/doAssign",
}

/**
 * 分页获取角色列表
 * @param param0 GetRoleListApiParams
 * @returns GetRoleListApiResponse
 */
export const getRoleListApi = ({
  page,
  limit,
  roleName,
}: GetRoleListApiParams) => {
  return request.get<any, GetRoleListApiResponse>(
    Api.getRoleList + `/${page}/${limit}`,
    {
      params: {
        roleName,
      },
    }
  );
};

/**
 * 添加角色
 * @param roleName 角色名称
 * @returns null
 */
export const addRoleApi = (roleName: string) => {
  return request.post<any, null>(Api.addRole, { roleName });
};

/**
 * 修改角色
 * @param data UpdateRoleApiParams
 * @returns null
 */
export const updateRoleApi = (data: UpdateRoleApiParams) => {
  return request.put<any, null>(Api.updateRole, data);
};

/**
 * 删除角色
 * @param id 角色Id
 * @returns null
 */
export const removeRoleApi = (id: string) => {
  return request.delete<any, null>(Api.removeRole + `/${id}`);
};

/**
 * 根据角色获取菜单
 * @param roleId 角色Id
 * @returns GetPermissionListApiResponse
 */
export const getRolePermissionListApi = (roleId: string) => {
  return request.get<any, GetRolePermissionListApiResponse>(
    Api.getRolePermissionList + `/${roleId}`
  );
};

/**
 * 给角色分配权限
 * @param roleId 角色Id
 * @param permissionId 菜单Id列表
 * @returns null
 */
export const assignRolePermissionListApi = (
  roleId: string,
  permissionId: string[]
) => {
  return request.post<any, null>(
    Api.assignRolePermissionList,
    {},
    {
      params: {
        roleId,
        permissionId: permissionId.join(","),
      },
    }
  );
};
